<?php

class Customer extends CI_Model
{	
	/*
	Determines if a given person_id is a customer
	*/
	
	private $db_customer = 'customer.';
	
	function exists($customer_id)
	{
		$this->db->from($this->db_customer.'customers');	
		$this->db->where('customers.person_id',$customer_id);
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}
	
	/*
	Returns all the customers
	*/
	function get_all($limit=10000, $offset=0)
	{
		$this->db->from($this->db_customer.'customers');		
		$this->db->where('deleted',0);
		$this->db->order_by("last_name", "asc");
		$this->db->limit($limit);
		$this->db->offset($offset);
		return $this->db->get();		
	}
	
	function count_all()
	{
		$this->db->from($this->db_customer.'customers');
		$this->db->where('deleted',0);
		return $this->db->count_all_results();
	}
		
		/*
	 * Gets information about a particular customer
	 */
	function get_info($customer_id) {
		$query = $this->db->get_where($this->db_customer.'customers', array('person_id' => $customer_id), 1);
		
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//create object with empty properties.
			$fields = $this->db->list_fields($this->db_customer.'customers');
			$person_obj = new stdClass;
			
			foreach ($fields as $field)
			{
				$person_obj->$field='';
			}
			
			return $person_obj;
		}
	}
	
	function get_number($customer_id) {
		$query = $this->db->get_where($this->db_customer.'customers', array('person_id' => $customer_id), 1);
	
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//create object with empty properties.
			$fields = $this->db->list_fields($this->db_customer.'customers');
			$person_obj = new stdClass;
				
			foreach ($fields as $field)
			{
				$person_obj->$field='';
			}
				
			return $person_obj;
		}
	}
	
	/*
	Gets information about multiple customers
	*/
	function get_multiple_info($customer_ids)
	{
		$this->db->from($this->db_customer.'customers');		
		$this->db->where_in('customers.person_id',$customer_ids);
		$this->db->order_by("last_name", "asc");
		return $this->db->get();		
	}
	
	/*
	Inserts or updates a customer
	*/
	function save(&$customer_data,$customer_id=false)
	{
		if (!$customer_id or !$this->exists($customer_id))
		{
			if ($this->db->insert($this->db_customer.'customers',$customer_data))
			{
				$customer_data['person_id']=$this->db->insert_id();
				return true;
			}
			
			return false;
		}
		
		$this->db->where('person_id', $customer_id);
		return $this->db->update($this->db_customer.'customers',$customer_data);
	}
	
	function add_point(&$point, &$customer_id=false) {
		if ($this->exists($customer_id)) {
			$current = $this->Customer->get_info($customer_id)->point;
			$data = array(
					'point' => $current + $point,
			);
			return $this->db->update($this->db_customer.'customers',$data,"person_id = ".$customer_id);
		}
	}
	
	function subtract_point(&$point, &$customer_id=false) {
		if ($this->exists($customer_id)) {
			$current = $this->Customer->get_info($customer_id)->point;
			$data = array(
					'point' => $current - $point,
			);
			return $this->db->update($this->db_customer.'customers',$data,"person_id = ".$customer_id);
		}
	}
	
	/*
	Deletes one customer
	*/
	function delete($customer_id)
	{
		$this->db->where('person_id', $customer_id);
		return $this->db->update($this->db_customer.'customers', array('deleted' => 1));
	}
	
	/*
	Deletes a list of customers
	*/
	function delete_list($customer_ids)
	{
		$this->db->where_in('person_id',$customer_ids);
		return $this->db->update($this->db_customer.'customers', array('deleted' => 1));
 	}
 	
 	/*
	Get search suggestions to find customers
	*/
	function get_search_suggestions($search,$limit=25)
	{
		$suggestions = array();
		
		$this->db->from($this->db_customer.'customers');	
		$this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		last_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");
		$this->db->order_by("last_name", "asc");		
		$by_name = $this->db->get();
		foreach($by_name->result() as $row)
		{
			$suggestions[]=$row->first_name.' '.$row->last_name;		
		}

		$this->db->from($this->db_customer.'customers');
		$this->db->where('deleted',0);		
		$this->db->like("phone_number",$search);
		$this->db->order_by("phone_number", "asc");		
		$by_phone = $this->db->get();
		foreach($by_phone->result() as $row)
		{
			$suggestions[]=$row->phone_number;		
		}
		
		$this->db->from($this->db_customer.'customers');
		$this->db->where('deleted',0);		
		$this->db->like("account_number",$search);
		$this->db->order_by("account_number", "asc");		
		$by_account_number = $this->db->get();
		foreach($by_account_number->result() as $row)
		{
			$suggestions[]=$row->account_number;		
		}
		
		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;
	}
	
	function get_customer_id($customer_number)
	{
		$this->db->from($this->db_customer.'customers');
		$this->db->where('account_number',$customer_number);
		$this->db->where('deleted',0); // Parq 131226
	
		$query = $this->db->get();
	
		if($query->num_rows()==1)
		{
			return $query->row()->person_id;
		}
	
		return false;
	}
	
	/*
	Get search suggestions to find customers
	*/
	function get_customer_search_suggestions($search,$limit=25)
	{
		$suggestions = array();
		
		$this->db->from($this->db_customer.'customers');	
		$this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		last_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");
		$this->db->order_by("last_name", "asc");		
		$by_name = $this->db->get();
		foreach($by_name->result() as $row)
		{
			$suggestions[]=$row->person_id.'|'.$row->first_name.' '.$row->last_name;		
		}
		
		$this->db->from($this->db_customer.'customers');	
		$this->db->where('deleted',0);		
		$this->db->like("account_number",$search);
		$this->db->order_by("account_number", "asc");		
		$by_account_number = $this->db->get();
		foreach($by_account_number->result() as $row)
		{
			$suggestions[]=$row->person_id.'|'.$row->account_number;
		}
		
		$this->db->from($this->db_customer.'customers');
		$this->db->where('deleted',0);
		$this->db->like("ssn",$search);
		$this->db->order_by("ssn", "asc");
		$by_ssn = $this->db->get();
		foreach($by_ssn->result() as $row)
		{
			$suggestions[]=$row->person_id.'|'.$row->ssn;
		}

		//only return $limit suggestions
		if(count($suggestions > $limit))
		{
			$suggestions = array_slice($suggestions, 0,$limit);
		}
		return $suggestions;

	}
	/*
	Preform a search on customers
	*/
	function search($search)
	{
		$this->db->from($this->db_customer.'customers');		
		$this->db->where("(first_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		last_name LIKE '%".$this->db->escape_like_str($search)."%' or 
		phone_number LIKE '%".$this->db->escape_like_str($search)."%' or 
		account_number LIKE '%".$this->db->escape_like_str($search)."%' or 
		CONCAT(`first_name`,' ',`last_name`) LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");		
		$this->db->order_by("last_name", "asc");
		
		return $this->db->get();	
	}

}
?>
